from flask_babel import gettext as _
from flask_wtf import Form
from wtforms import TextAreaField, HiddenField, BooleanField, PasswordField, StringField, ValidationError
from wtforms.validators import DataRequired, InputRequired, EqualTo, Email, Length

from ..models import User


class LoginForm(Form):
    username = StringField('Username or email address', validators=[
                      DataRequired(message='You must provide an email or username')])
    password = PasswordField('Password', validators=[
                      DataRequired(message='You must input your password')])
    remember = BooleanField('Remember me', default=False)

    next = HiddenField()


class SignupForm(Form):
    username = StringField(_("Username"), validators=[
                         DataRequired(message=_("Username required"))])
    nickname = StringField(_("Nickname"), validators=[
                         DataRequired(message=_("Nickname required"))])

    password = PasswordField(_("Password"), validators=[
                             InputRequired(message=_("Password required"))])

    password_again = PasswordField(_("Password again"), validators=[InputRequired(),
                                   EqualTo("password", message=\
                                            _("Passwords don't match"))])

    email = StringField(_("Email address"), validators=[
                      DataRequired(message=_("Email address required")),
                      Email(message=_("A valid email address is required"))])
    #
    # code = StringField(_("Signup Code"))

    next = HiddenField()

    def validate_username(self, field):
        user = User.query.filter_by(username=field.data).first()
        if user:
            raise ValidationError, _("This username is taken")

    def validate_email(self, field):
        user = User.query.filter_by(email=field.data).first()
        if user:
            raise ValidationError, _("This email is taken")


class ProfileForm(Form):
    nickname = StringField('Nickname', validators=[Length(0, 64)])
    location = StringField('Location', validators=[Length(0, 64)])
    about_me = TextAreaField('About me')


class PostForm(Form):
    title = StringField(_("Title"), validators=[Length(0, 64)])
    content = TextAreaField('Content')
    tags = StringField(_("Tags"))


class CommentForm(Form):
    body = TextAreaField('Body', validators = [DataRequired()])
